<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Ajax
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Ajax_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function del_restore_item($table, $id, $mode, $name = '')
	{
		$this->db->where('id', $id);
		if ($mode == 'del') $this->db->update($table, array('deleted' => 1));
		if ($mode == 'restore') $this->db->update($table, array('deleted' => 0));
		if ($mode == 'remove') $this->db->delete($table);
		if ($table != 'userlogs' && $name) write_log($mode.'_'.$table, $name);
		return true;
	}

	public function change_config($keyword, $value)
	{
		$this->db->where('keyword', $keyword);
		$this->db->update('config', array('value' => $value));
		write_log('update_config', $keyword);
		return true;
	}

	public function change_status($table, $id, $field, $status, $one_select = 0)
	{
        if ($one_select == 1) $this->db->update($table, array($field => 0));
        $this->db->where('id', $id);
		$this->db->update($table, array($field => $status));
	}

	public function delete_image($table, $id, $field)
	{
		$this->db->select($field);
		$this->db->where('id', $id);
		$query = $this->db->get($table);
		if ($query->num_rows() > 0)
		{
			$data = $query->row_array();
	        $this->db->where('id', $id);
	        $this->db->update($table, array($field => ''));
	        return $data[$field];
		}
		return false;
	}

	public function sort_order($table, $IDs, $field = '', $cat = '', $orderby = '', $ordermode = '')
	{
		if ($orderby == '') $orderby = 'sort_order';
		if ($ordermode == '') $ordermode = 'desc';
		$this->db->select('id');
		if ($field != '') $this->db->where($field, 1);
		if ($cat != '') $this->db->where('cat', $cat);
		$query = $this->db->get($table);
		$rows = $query->num_rows();
		$IDs = explode(',', $IDs);
		for ($i = 0; $i < count($IDs); $i++)
		{
			$this->db->where('id', $IDs[$i]);
			$this->db->update($table, array($orderby => ($ordermode == 'asc' ? $i : $rows - $i)));
		}
		return true;
	}

	public function sort_nestable($table, $data, $id)
	{
	    if (!is_array($data) || !$table || !$id)
	    {
	          return false;
	    }
	    $this->db->where('id', $id);
	    $this->db->update($table, $data);
	    return true;
	}

	public function update_price($id, $field, $table, $price)
	{
		$this->db->where('id', $id);
		$this->db->update($table, array($field => $price));
		return number_format($price);
	}

	public function check_code($code, $table, $id = '')
	{
		$this->db->where('code', $code);
		$this->db->where('deleted', 0);
		if ($id > 0) $this->db->where('id <>', $id);
		if ($this->db->get($table)->num_rows() > 0)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	public function load_product($cat, $id = '')
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		if ($id > 0) $this->db->where('id <>', $id);
		$this->db->like('cat', '"'.$cat.'"');
		$this->db->where_not_in('id', $this->input->post('list', true));
	    $this->db->select('id, name_vn, img1');
	    $query = $this->db->get('products');
	    if ($query->num_rows() > 0)
	    {
	    	return $query->result_array();
	    }
	    else
	    {
	    	return false;
	    }
	}

	public function check_cat_child($table, $parent)
	{
		if (!$parent || !$this->db->field_exists('parent', $table))
		{
			return 0;
		}
		$this->db->where('parent', $parent);
		$this->db->where('deleted', 0);
		$this->db->select('id');
		return $this->db->get($table)->num_rows();
	}
}

/* End of file ajax_model.php */
/* Location: ./application/models/ajax_model.php */